package com.yueguang.springai.service;

import com.yueguang.springai.context.OrderContext;
import com.yueguang.springai.inode.impl.*;
import com.yueguang.springai.model.dto.OrderDO;
import com.yueguang.springai.model.param.OrderParam;
import com.yueguang.springai.model.vo.OrderCreateRequestVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;

@Service
public class OrderService implements IOrderService{

    @Resource
    private ANodeComponent aNodeComponent;
    @Resource
    private BNodeComponent bNodeComponent;
    @Resource
    private CNodeComponent cNodeComponent;
    @Resource
    private DNodeComponent dNodeComponent;
    @Resource
    private ENodeComponent eNodeComponent;
    @Resource
    private FNodeComponent fNodeComponent;
    @Resource
    private HNodeComponent hNodeComponent;


    @Override
    /**
     * 创建订单
     * 根据传入的订单创建请求VO来创建订单
     * 此方法首先构建订单参数，然后根据订单号是否为"11111"来决定处理订单的节点组件流程
     * @param vo 订单创建请求VO，包含创建订单所需的信息
     * @return 返回创建的订单DO对象，当前方法中未实现返回逻辑
     */
    public OrderDO createOrder(OrderCreateRequestVO vo) {
        // 构建订单参数，为后续的节点组件处理做准备
        final OrderParam orderParam = this.buildOrderParam(vo);
        // 初始化订单上下文，用于在节点组件处理过程中传递和存储订单相关信息
        final OrderContext orderContext = OrderContext.builder().orderId(vo.getOrderNo()).executeRuleList(new ArrayList<>()).build();

        // 根据订单号是否为"11111"来决定处理订单的节点组件流程
       /* if (Objects.equals(vo.getOrderNo(), "11111")){
            // 当订单号为"11111"时，按照特定的节点组件顺序处理订单数据
            aNodeComponent.handleData(orderContext, orderParam);
            cNodeComponent.handleData(orderContext, orderParam);
            dNodeComponent.handleData(orderContext, orderParam);
            fNodeComponent.handleData(orderContext, orderParam);
            hNodeComponent.handleData(orderContext, orderParam);
        }else {
            // 当订单号不为"11111"时，按照另一套节点组件顺序处理订单数据
            bNodeComponent.handleData(orderContext, orderParam);
            cNodeComponent.handleData(orderContext, orderParam);
            eNodeComponent.handleData(orderContext, orderParam);
            hNodeComponent.handleData(orderContext, orderParam);
        }*/
        if (Objects.equals(vo.getOrderNo(), "11111")){
            // 当订单号为"11111"时，按照特定的节点组件顺序处理订单数据
            aNodeComponent.handleData(orderContext, orderParam);
        }else {
            // 当订单号不为"11111"时，按照另一套节点组件顺序处理订单数据
            bNodeComponent.handleData(orderContext, orderParam);
        }

        return this.buildOrderDO(orderContext);
    }


    protected OrderParam buildOrderParam(OrderCreateRequestVO requestVO) {
        // 这里根据实际需求构建 OrderParam 对象
        OrderParam orderParam = new OrderParam();
        // 从 requestVO 中获取相关数据设置到 orderParam 中
        orderParam.setOrderNo(requestVO.getOrderNo());
        orderParam.setOrderType(requestVO.getOrderType());
        return orderParam;
    }

    protected OrderDO buildOrderDO(OrderContext orderContext) {
        // 这里根据实际需求构建 OrderDO 对象
        OrderDO orderDO = new OrderDO();
        // 从 orderContext 中获取相关数据设置到 orderDO 中
        orderDO.setOrderId(orderContext.getOrderId());
        return orderDO;
    }
}
